package com.advert.presentation.util;

import com.advert.presentation.util.localization.LabelsMap;
import com.advert.util.Sortable;

import java.text.Collator;
import java.util.Comparator;

public class EntityComparator implements Comparator<Sortable> {
    private LabelsMap labelsMap;

    public EntityComparator(LabelsMap labelsMap) {
        this.labelsMap = labelsMap;
    }


    public int compare(Sortable o1, Sortable o2) {
        if (o1.getSortingValue() == null && o2.getSortingValue() == null) {
            return 0;
        } else if (o1.getSortingValue() == null) {
            return -1;
        } else if (o2.getSortingValue() == null) {
            return 1;
        } else {
            String o1Value = labelsMap.get(o1.getSortingValue()).toString();
            String o2Value = labelsMap.get(o2.getSortingValue()).toString();

            return Collator.getInstance().compare(o1Value, o2Value);
        }
    }
}
